﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;


namespace CARMS.Report
{
    [Function("Report.ReportConfirm")]
    public partial class ReportConfirm : PageBase
    {
        private int CurrentReportHeaderID
        {
            get
            {
                return GetIntByParameter("ID");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var boolConfirm = CheckPageEventPermission(DictionaryCategoryConst.ButtonPermission.Confirm);
                if (boolConfirm)
                {
                    LoadDefault();
                }
                else
                {
                    btnApprove.Visible = false;
                    btnReject.Visible = false;
                    Alert("Sorry, you do not have the permission to confirm this report.");
                }
            }
        }

        private void LoadDefault()
        {
            var reportHeader = new ReportHeaderEntity(CurrentReportHeaderID);
            if (!reportHeader.IsNew)
            {
                txtConfirmComment.Text = reportHeader.Comment;
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            var reportHeader = new ReportHeaderEntity(CurrentReportHeaderID);
            if (!reportHeader.IsNew)
            {
                reportHeader.Comment = txtConfirmComment.Text.Trim();
                reportHeader.Status = DictionaryCategoryConst.ReportStatus.Confirmed;
                if (reportHeader.Save())
                {
                    MessageBusiness.DeleteMessageByReportHeaderID(reportHeader.ReportHeaderId,
                                                                  DictionaryCategoryConst.ButtonPermission.Confirm);
                    MessageBusiness.SendMessageByReportID(reportHeader.ReportHeaderId,
                                                                DictionaryCategoryConst.ButtonPermission.Confirm);
                    RediretResolveUrl(SiteUrls.Message.MessageList);
                }
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            var reportHeader = new ReportHeaderEntity(CurrentReportHeaderID);
            if (!reportHeader.IsNew)
            {
                reportHeader.Comment = txtConfirmComment.Text.Trim();
                reportHeader.Status = DictionaryCategoryConst.ReportStatus.Rejected;
                if (reportHeader.Save())
                {
                    MessageBusiness.DeleteMessageByReportHeaderID(reportHeader.ReportHeaderId,
                                                        DictionaryCategoryConst.ButtonPermission.Reject);
                    MessageBusiness.SendMessageByReportID(reportHeader.ReportHeaderId,
                                                                DictionaryCategoryConst.ButtonPermission.Reject);
                    RediretResolveUrl(SiteUrls.Message.MessageList);
                }
            }
        }
    }
}
